package com.study.example.gameserver.module.ranklist.controller;

import com.sparrow.common.ranklist.RankElement;
import com.sparrow.common.ranklist.RankList;
import com.sparrow.event.annotation.EventReceiver;
import com.sparrow.example_message.rank.req.ReqRankList;
import com.sparrow.example_message.rank.resp.RespRankList;
import com.sparrow.example_message.rank.vo.RankVo;
import com.sparrow.socket.tcp.anno.PacketHandler;
import com.study.example.gameserver.module.player.entity.Player;
import com.study.example.gameserver.module.ranklist.RankService;
import com.study.example.gameserver.module.ranklist.domain.RankType;
import com.study.example.gameserver.module.ranklist.handler.RankHandler;
import com.study.example.gameserver.module.ranklist.manager.RankManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@PacketHandler
@Component
public class RankController {

    @Autowired
    RankManager rankManager;

    @Autowired
    RankService rankService;

    @EventReceiver
    public void reqRankList(Player player, ReqRankList req) {
        RankType rankType = RankType.of(req.getRankType());
        if (rankType == null) {
            return;
        }
        RankList rankList = rankManager.getRankList(rankType);
        if (rankType == null) {
            return;
        }
        List<RankElement> itemList = rankList.getRankList();
        RankHandler handler = rankService.getHandler(rankType);
        if (handler == null) {
            return;
        }

        List<RankVo> results = handler.toRankVoList(itemList);
        RespRankList resp = new RespRankList();
        resp.setRankType(req.getRankType());
        resp.setRankList(results);
        player.sendPacket(resp);
    }

}
